package com.yzdbx.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.SystemPropsUtil;
import com.yzdbx.task.MainTask;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: SERVEVICE-DATA-GEN
 * @BelongsPackage: com.yzdbx.utils
 * @Author: lk
 * @CreateTime: 2025-05-14
 * @Description: TODO
 * @Version: 1.0
 */
public class GenUtil {

    public static final ThreadLocal<LocalDate> CUR_DATE = new ThreadLocal<>();
    public static final ThreadLocal<Timestamp> CUR_TIME_STAMP = new ThreadLocal<>();
    public static final String IS_INIT = SystemPropsUtil.get("IS_INIT", "Y");
    public static final String CYCLE = SystemPropsUtil.get("CYCLE", "02:00:00");

    public static String getNo(String prefix, int num) {
        // 修改单号生成逻辑：每天从01开始，格式为 prefix + yyyyMMdd + 4位序号
        String dateStr = CUR_DATE.get().toString().replace("-", "");
        String numStr = String.format("%04d", num); // 4位数字，不足补0
        return prefix + dateStr + numStr;
    }
// 在IDEA中创建需求注释
    /**
     * @ai-request 数据访问需求：
     * 1. 根据商品ID查询最近30天订单量
     * 2. 需要分页处理（每页20条）
     * 3. 包含订单状态过滤（已支付）
     * 4. 返回字段：订单号、购买数量、下单时间
     */


    public static int getNoRepeatId(Integer id, List<Integer> existIds, List<Integer> mainIds) {
        while (existIds.contains(id)) id = RandomUtil.randomEle(mainIds);
        return id;
    }

    public static void setCurDate(LocalDate curDate) {
        CUR_DATE.set(curDate);
    }

    public static void setCurTimeStamp(Timestamp curTimeStamp) {
        CUR_TIME_STAMP.set(curTimeStamp);
    }


    public static Date ramdonDate() {
        LocalDate start = MainTask.start;
        LocalDate end = MainTask.end;
        List<LocalDate> list = new ArrayList<>();
        while (!start.isAfter(end)) {
            list.add(start);
            start = start.plusDays(1);
        }
        LocalDate localDate = RandomUtil.randomEle(list);
        return Date.valueOf(localDate);
    }
}
